package com.huluobo.a2112mvvmdouyindemo.shop;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.huluobo.a2112mvvmdouyindemo.R;
import com.huluobo.a2112mvvmdouyindemo.adapter.ShopGoodsAdapter;
import com.huluobo.a2112mvvmdouyindemo.cart.CartActivity;
import com.huluobo.a2112mvvmdouyindemo.databinding.ActivityShopBinding;
import com.huluobo.mvvm_base.bean.AddToCartBeanResult;
import com.huluobo.mvvm_base.bean.GoodsBeanResult;
import com.huluobo.mvvm_base.view.BaseMVVMActivity;

import java.util.ArrayList;
import java.util.List;

public class ShopActivity extends BaseMVVMActivity<ActivityShopBinding, ShopViewModel> {

    private List<GoodsBeanResult.GoodsBean> goodsBeanList = new ArrayList<>();

    private ShopGoodsAdapter shopGoodsAdapter;

    @Override
    protected void initData() {
        viewModel.getGoodsList(0, 1, 20);

        viewModel.getGoodsListSuccess.observe(this, goodsBeanResult -> {
            if (goodsBeanResult.getCode() == 200) {
                goodsBeanList.clear();
                goodsBeanList.addAll(goodsBeanResult.getData());
                shopGoodsAdapter.notifyDataSetChanged();
            }
        });

        viewModel.getGoodsListFailed.observe(this, s -> {
            Log.i("TagA", "获取商品列表失败:" + s);
        });

        viewModel.addToCartSuccess.observe(this, addToCartBeanResult -> {
            ToastUtils.showLong(addToCartBeanResult.getMessage());
        });

        viewModel.addToCartFailed.observe(this, s -> {
            Log.i("TagA", "添加购物车失败:" + s);
        });
    }

    @Override
    protected void initView() {
        dataBinding.shopRv.setLayoutManager(new GridLayoutManager(this, 2));
        shopGoodsAdapter = new ShopGoodsAdapter(goodsBeanList);
        dataBinding.shopRv.setAdapter(shopGoodsAdapter);

        shopGoodsAdapter.addChildClickViewIds(R.id.item_shop_add_to_cart);

        shopGoodsAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            switch (view.getId()) {
                case R.id.item_shop_add_to_cart:
                    int goodsId = goodsBeanList.get(position).getId();
                    //发起请求,添加购物车
                    viewModel.addToCart(goodsId, 1);
            }
        });

        dataBinding.shopCartIv.setOnClickListener(v -> {
            Intent intent = new Intent(this, CartActivity.class);
            startActivity(intent);
        });
    }

    @Override
    protected Class<ShopViewModel> getViewModel() {
        return ShopViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_shop;
    }
}